import axios from 'axios';
import type { QuestionLevel } from '../common/interfaces.ts'
// const API_BASE_URL = 'http://localhost:3000/question-levels';
const API_BASE_URL = `${import.meta.env.VITE_API_BASE_URL}/resources/question-levels`


// 获取难度列表
export const fetchQuestionLevels = async () => {
  try {
    const response = await axios.get(API_BASE_URL);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('获取难度数据失败:', error);
    throw error;
  }
};

// 添加难度
export const addQuestionLevel = async (questionLevel: QuestionLevel) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/`, questionLevel);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('添加难度失败:', error);
    throw error;
  }
};

// 编辑难度
export const updateQuestionLevel = async (id: number, questionLevel: QuestionLevel) => {
  try {
    const response = await axios.patch(`${API_BASE_URL}/${id}/`, questionLevel);
    // return response.data;
	return response;
  } catch (error) {
    // console.error('编辑难度失败:', error);
    throw error;
  }
};

// 删除难度
export const deleteQuestionLevel = async (id: number) => {
  try {
    const response = await axios.delete(`${API_BASE_URL}/${id}/`);
	return response;
  } catch (error) {
    // console.error('删除难度失败:', error);
    throw error;
  }
};

export const searchQuestionLevel = async (searchData: object) => {
	try {
		const response = await axios.get(`${API_BASE_URL}?search_data=${JSON.stringify(searchData)}`);
		return response;
	} catch (error) {
	  // console.error('删除难度失败:', error);
	  throw error;
	}
}

export const searchPaginationQuestionLevel = async (page_size:number, current_page:number, searchData:object) => {
	/**
	 * 通过分页获取查询对象
	*/
	const response = await axios.get(`${API_BASE_URL}?page_size=${page_size}&current_page=${current_page}&search_data=${JSON.stringify(searchData)}`);
	return response;
}